package com.order.controller;

import com.usian.appraise.Appraise;
import com.usian.order.OrderUser;
import com.order.service.UserService;
import com.usian.order.Order;
import com.order.service.OrderService;
import com.usian.order.TravelRequest;
import com.usian.travelSchedule.TravelSchedule;
import com.usian.user.User;
import com.usian.vo.Result;
import com.usian.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @PostMapping("/scenic")
    public ResultVo getScenicOrder(@RequestBody String addres) {
        return ResultVo.ok(orderService.selectScenicOrder(addres));
    }
    @GetMapping("/{id}/{status}")
/**
 * 根据用户ID获取订单信息
 * 此方法首先根据提供的用户ID从订单服务中选择订单，然后为每个订单获取对应的用户信息，
 * 并将这些用户信息编译成一个列表返回如果找不到订单，则返回错误的ResultVo
 *
 * @param id 用户ID，用于获取该用户的订单信息
 * @return ResultVo 包含用户列表的ResultVo对象，如果没有找到订单，则返回错误的ResultVo
 */
    public ResultVo getOrder(@PathVariable Integer id, @PathVariable Integer status) {
        // 参数验证
        if (id == null || status == null) {
            return ResultVo.error("Invalid parameters");
        }
        // 根据用户ID从订单服务中选择订单
        // status为0时，返回出行定制订单，为1时，返回咨询服务订单
        status = status == 0 ? 1 : 2;
        List<Order> orders;
        if (status ==1){
            orders = orderService.selectOrder(id, status);
        }else {
            orders = orderService.selectTakeOrders(id, status);
            if (orders.size()<=0){
                orders = orderService.selectOrder(id, status);
            }
        }
        // 遍历订单列表，为每个订单设置相关信息
        for (Order order : orders) {
            // 根据订单的地址ID查询旅行安排
            TravelSchedule travelSchedule = orderService.selectTravel(order.getAddressId());
            // 如果旅行安排不为空，则进一步处理
            if (travelSchedule != null){
                // 设置订单的地址名称
                order.setAddressname(travelSchedule.getName());
                // 根据旅行安排中的设计师ID查询用户信息
                User user = userService.selectById(travelSchedule.getDesignerId());
                if (user!=null){
                    // 设置订单的图片为设计师的图片
                    order.setImage(user.getImage());
                    // 设置订单的设计师ID
                    order.setDesignerId(travelSchedule.getDesignerId());
                }
                //若行程无则转为咨询订单
            }else {
                //获取咨询设计师信息
                User user1 = userService.selectById(order.getAcceptId());
                if (user1!=null){
                    order.setImage(user1.getImage());
                }
            }
        }
        // 返回包含用户列表的ResultVo
        return ResultVo.ok(orders);
    }
    @GetMapping("/deletestatus/{id}")
    public ResultVo deleteStatus(@PathVariable Integer id) {
        // 参数验证
        if (id == null) {
            return ResultVo.error("Invalid parameters");
        }
        // 根据订单ID删除订单状态
        int orderStatus = 0;
        int i = orderService.deleteStatus(id,orderStatus);
        // 返回删除结果
        return i > 0 ? ResultVo.ok() : ResultVo.error("删除失败");
    }


    /**
     * 接口：添加咨询订单
     */
    @PostMapping("/saveOrder")
    public Result saveOrder(@RequestBody Order order) {
        int i = orderService.saveOrder(order);
        if (i > 0){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    @PostMapping("/adviceByUserId/{userId}")
    public Result adviceByUserId(@PathVariable Integer userId){
        List<Order> list = orderService.selectTakeOrder(userId,2);
        return Result.success(list);
    }


    @GetMapping("/confirm/{id}")
    public ResultVo confirm(@PathVariable Integer id) {
        Integer i =  orderService.selectTravelOrderStatuslook(id);
        if (i!=null){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
   @GetMapping("/payselect/{tradeid}/{id}")
    public ResultVo payselect(@PathVariable Integer tradeid,@PathVariable Integer id) {
       // 根据订单ID查询订单信息
       Integer order = orderService.selectById(tradeid,id);
       if (order!=null){
           return ResultVo.ok(order);
       }else {
           return ResultVo.error();
       }
   }
   @PostMapping("/appraise/{designerId}/{userid}/{orderid}")
    public Result appraise(@PathVariable Integer designerId, @PathVariable Integer userid, @PathVariable Integer orderid,@RequestBody Appraise appraise){
        return Result.success(orderService.appraise(designerId,userid,orderid,appraise));
    }

    /**
     * 接口：查询未接单的咨询服务订单
     */
    @GetMapping("/getUnacceptedOrders/{orderStatus}")
    public ResultVo getUnacceptedOrders(@PathVariable Integer orderStatus) {
        List<Order> orders = orderService.getUnacceptedOrders(orderStatus);
        return ResultVo.ok(orders);
    }

    /**
     * 接口：修改咨询订单状态
     */
    @GetMapping("/updateByStatus/{id}/{orderStatus}/{userId}")
    public ResultVo updateByStatus(@PathVariable Integer id, @PathVariable Integer orderStatus,@PathVariable Integer userId) {
        int i = orderService.updateByStatus(id, orderStatus,userId);
        return i > 0 ? ResultVo.ok() : ResultVo.error();
    }
    @GetMapping("/skipPayment/{id}")
    public Result skipPayment(@PathVariable Integer id){
        return Result.success(orderService.selectPay(id));
    }



    @GetMapping("/selectAdviceByAcceptId/{userId}")
    public Result selectAdviceByAcceptId(@PathVariable Integer userId){
        List<Order> list = orderService.selectTakeOrders(userId,2);
        return Result.success(list);
    }


}
